package com.jadcon.bedbreakfast.domain;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/spring-security.xml", "/spring-service.xml" })
public class ReservationServiceTestIntegration {

	private Logger logger = Logger
			.getLogger(ReservationServiceTestIntegration.class);

    // reservation repository is used to bypass security 

	@Autowired
	ReservationRepository reservationRepository;

	@Test
	public void getReservationList() {
		List<Reservation> reservationList = reservationRepository.getReservationList();
		if(logger.isDebugEnabled()){
			logger.debug("getReservationList() - Reservation list size is: "
					+ reservationList.size());
		}
	}

	@Test
	public void saveReservation() {
		int reservationNumber = 0;
		int totalNumberOfGuests = 2;
		int roomNumber = 105;
		Date checkIn = new Date();
		Date checkOut = new Date();
		String username = "87jkgm,ng23t8sf9dgfl;kjw56";

		Reservation reservation = new Reservation(reservationNumber,
				roomNumber, totalNumberOfGuests, checkIn, checkOut, username);

		reservationRepository.save(reservation);
		
		reservationRepository.getReservationNumberForNewlyCreatedReservation(reservation);

	}
	
	
	@Test
	public void findByNumber() {
		List<Reservation> reservationList = reservationRepository.getReservationList();
		for (Reservation reservation : reservationList ) {
			if (reservation.getTotalNumberOfGuests()==2 &&
					reservation.getRoomNumber()==105 &&
					reservation.getUsername().equals("87jkgm,ng23t8sf9dgfl;kjw56")) {
			int number =	reservation.getReservationNumber();
			reservationRepository.findByNumber(number);
			}
		}
	}
	
	@Test
	public void findReservationByRoom() {
		int roomNumber = 105;
		reservationRepository.findReservationByRoom(roomNumber);
	}
	
	@Test
	public void findByStartDate(){
		String username = "87jkgm,ng23t8sf9dgfl;kjw56";
		reservationRepository.findReservationByStartDate(new Date());
		reservationRepository.findReservationByStartDate(new Date(), username);
	}
	
	@Test
	public void findByUsername(){
		String username = "87jkgm,ng23t8sf9dgfl;kjw56";
		reservationRepository.findByUsername(username);
	}
	
	@Test
	public void delete(){
		List<Reservation> reservationList = reservationRepository.getReservationList();
		for (Reservation reservation : reservationList ) {
			if (reservation.getTotalNumberOfGuests()==2 &&
					reservation.getRoomNumber()==105 &&
					reservation.getUsername().equals("87jkgm,ng23t8sf9dgfl;kjw56")) {
			int number =	reservation.getReservationNumber();
		    reservationRepository.delete(number);
			}
		}
	}
}
